package it.greg.findparty.util;

import java.net.URLEncoder;

public class Distance {
	
	private HtmlUtil htmlUtil;
	
	public Distance() {
		this.htmlUtil = new HtmlUtil();
	}
	
	public int calculateBetweenAddress(String addrSource, String addrDestination) {
		int distance, i, f;
		addrSource = URLEncoder.encode(addrSource);
		addrDestination = URLEncoder.encode(addrDestination);
		String query = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+addrSource+"&destinations="+addrDestination+"&language=en-EN&sensor=false";
		String html = this.htmlUtil.getHTML(query);
		//Log.i("html", html);
		try {
			i = html.indexOf("value");
			f = html.indexOf("\n",i);
			String result = html.substring(i+9,f);
			distance = Integer.parseInt(result);
		} catch(Exception e){
			return -1;
		}
		return distance;
	}

}
